using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private breakForm bForm;

        private void Form1_Load(object sender, EventArgs e)
        {
            bForm = new breakForm();
            bForm.setPForm(this);
            Hide();
            readConfig();
            lastBreak1 = DateTime.Now;
            lastBreak2 = DateTime.Now;
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            interval1.Enabled = checkBox2.Checked;
            time1.Enabled = checkBox2.Checked;
        }

        private void E_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            breakBox2.Enabled = checkBox2.Checked;
        }

        private void checkBox1_CheckedChanged_1(object sender, EventArgs e)
        {
            breakBox1.Enabled = checkBox1.Checked;
        }

        private DateTime lastBreak1;
        private DateTime lastBreak2;

        private void button1_Click(object sender, EventArgs e)
        {
            timer1Interval = (int)interval1.Value;
            timer2Interval = (int)interval2.Value;

            timer1Time = (int)time1.Value;
            timer2Time = (int)time2.Value;

            timer1Enabled = checkBox1.Checked;
            timer2Enabled = checkBox2.Checked;

            Hide();
            saveConfig();
            if (firstRun)
            {
                notifyIcon1.ShowBalloonTip(10000, "Quick Tip", "If you want to take a break earlier, right click here.", ToolTipIcon.Info);
                firstRun = false;
            }
        }

        private int timer1Interval;
        private int timer1Time;
        private bool timer1Enabled;
        private int timer2Interval;
        private int timer2Time;
        private bool timer2Enabled;

        private bool firstRun = false;

        private void readConfig()
        {
            RegistryKey regKey = Registry.CurrentUser.
                        OpenSubKey(@"Software\Venture Impossible\Time Break", true);
            if (regKey == null)
            {
                regKey = Registry.CurrentUser.CreateSubKey(@"Software\Venture Impossible\Time Break");
                firstRun = true;
            }
            timer1Enabled = (int)regKey.GetValue("Timer1Enabled",1) == 1;
            timer2Enabled = (int)regKey.GetValue("Timer2Enabled",1) == 1;

            timer1Interval = (int)regKey.GetValue("Timer1Interval",15);
            timer2Interval = (int)regKey.GetValue("Timer2Interval",60);

            timer1Time = (int)regKey.GetValue("Timer1Time",15);
            timer2Time = (int)regKey.GetValue("Timer2Time",60);

            time1.Value = timer1Time;
            time2.Value = timer2Time;

            interval1.Value = timer1Interval;
            interval2.Value = timer2Interval;

            checkBox1.Checked = timer1Enabled;
            breakBox1.Enabled = timer1Enabled;
            checkBox2.Checked = timer2Enabled;
            breakBox2.Enabled = timer2Enabled;

            regKey.Close();

            regKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
            if (firstRun || regKey.GetValue("TimeBreak") != null)
                startupCheckBox.Checked = true;
            regKey.Close();
        }

        private void saveConfig()
        {
            RegistryKey regKey = Registry.CurrentUser.
                OpenSubKey(@"Software\Venture Impossible\Time Break", true);

            regKey.SetValue("Timer1Enabled", checkBox1.Checked, RegistryValueKind.DWord);
            regKey.SetValue("Timer2Enabled", checkBox2.Checked, RegistryValueKind.DWord);

            regKey.SetValue("Timer1Interval", interval1.Value, RegistryValueKind.DWord);
            regKey.SetValue("Timer2Interval", interval2.Value, RegistryValueKind.DWord);

            regKey.SetValue("Timer1Time", time1.Value, RegistryValueKind.DWord);
            regKey.SetValue("Timer2Time", time2.Value, RegistryValueKind.DWord);

            regKey.Close();

            regKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
            if (startupCheckBox.Checked)
            {
                regKey.SetValue("TimeBreak", "\"" + Application.ExecutablePath + "\"");
            }
            else
            {
                regKey.DeleteValue("TimeBreak",false);
            }
            regKey.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Hide();
            readConfig();
        }

        private void Form1_HelpButtonClicked(object sender, CancelEventArgs e)
        {

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (CanClose == false)
            {
                Hide();
                e.Cancel = true;
                readConfig();
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            notifyIcon1.Visible = false;

        }

        private void checkBox2_CheckedChanged_1(object sender, EventArgs e)
        {
            breakBox2.Enabled = checkBox2.Checked;
        }

        private bool CanClose = false;

        private void exitTimeBreakToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CanClose = true;
            Close();
        }

        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Show();
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {

        }

        private void takeBreakNowToolStripMenuItem_Click(object sender, EventArgs e)
        {
            lastBreak1 = DateTime.Now;
            lastBreak2 = DateTime.Now;
            takeBreak(timer2Time);
        }

        private void takeBreak(int seconds)
        {
       
            warningShown = false;
            bForm.DoIt(seconds);
        }

        private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (Visible == false)
                {
                    Show();
                    BringToFront();
                    Focus();
                }
                else
                {
                    Hide();
                }
            }
        }

        bool warningShown = true;

        private void timer_Tick(object sender, EventArgs e)
        {
            int remainingToBreak1 = 999;
            int remainingToBreak2 = 999;

            if (timer1Enabled)
            {
                remainingToBreak1 = timer1Interval - (DateTime.Now - lastBreak1).Minutes;
                if (remainingToBreak1 == 1 && !warningShown)
                {
                    notifyIcon1.BalloonTipText = "A " + timer1Time + " second break is coming in 1 minute.";
                    notifyIcon1.ShowBalloonTip(5000);
                    warningShown = true;
                }
                else if (remainingToBreak1 < 1)
                {
                    lastBreak1 = DateTime.Now;
                    takeBreak(timer1Time);
                }
            }

            if (timer2Enabled)
            {
                remainingToBreak2 = timer2Interval - (DateTime.Now - lastBreak2).Minutes;
                if (remainingToBreak2 == 1 && !warningShown)
                {
                    notifyIcon1.BalloonTipText = "A " + timer2Time + " second break is coming in 1 minute.";
                    notifyIcon1.ShowBalloonTip(5000);
                    warningShown = true;
                }
                else if (remainingToBreak2 < 1)
                {
                    lastBreak2 = DateTime.Now;
                    lastBreak1 = DateTime.Now;
                    takeBreak(timer2Time);
                }
            }

            int timeToNextBreak = Math.Min(remainingToBreak2, remainingToBreak1);
            notifyIcon1.Text = "Next break in " + timeToNextBreak + (timeToNextBreak == 1 ? " minute." : " minutes.");
        }

        private bool justStarted = true;

        private void Form1_Shown(object sender, EventArgs e)
        {
            if (justStarted && !firstRun)
            {
                Hide();
                justStarted = false;
            }
        }

        internal void resetTimers()
        {
            lastBreak1 = DateTime.Now;
            lastBreak2 = DateTime.Now;
        }

    }
}